package swing.project.standard;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;

import work.utils.WorksUtils;

import com.h819.core.file.MyFileUtils;
import com.h819.core.file.pdf.itext.PDFUtils;
import com.h819.core.utils.MyDateUtils;
import com.h819.core.utils.MyMessageException;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

/**
 * @Title: Jpanel5.java
 * @Description: TODO(添加描述)
 * @author H819
 * @date 2010-10-10
 * @version V1.0
 */
public class Jpanel5 extends JPanel {

	/**
	 * @wbp.nonvisual location=32,362
	 */
	private final JFileChooser fileChooser = new JFileChooser();
	/**
	 * @wbp.nonvisual location=162,362
	 */
	private final JFileChooser fileChooserpdf = new JFileChooser();
	private JLabel statuslable;
	private JButton buttonChooseFile;
	private static Logger log = Logger.getLogger(Jpanel5.class);
	private JLabel infolabel = new JLabel("");
	private JButton buttonChoosePDFFolder;
	/**
	 * @wbp.nonvisual location=282,352
	 */
	private final JFileChooser fileChooserdecryptPdf = new JFileChooser();

	/**
	 * Create the panel.
	 */
	public Jpanel5() {

		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.BUTTON_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.GLUE_COLSPEC, },
				new RowSpec[] { FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.PREF_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.PREF_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.PREF_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.GLUE_ROWSPEC, }));

		fileChooser.setCurrentDirectory(new File("d:\\"));

		fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("*.txt",
				"txt"));

		fileChooserpdf.setCurrentDirectory(new File("d:\\"));

		JLabel maininfo = new JLabel(
				ResourceBundle
						.getBundle("swing.project.standard.messages").getString("Jpanel5.maininfo.text")); //$NON-NLS-1$ //$NON-NLS-2$
		add(maininfo, "2, 4, 27, 1");

		buttonChoosePDFFolder = new JButton(
				ResourceBundle
						.getBundle("swing.project.standard.messages").getString("Jpanel5.btnPdf.text")); //$NON-NLS-1$ //$NON-NLS-2$
		buttonChoosePDFFolder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				fileChooserpdf
						.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

				int returnValue = fileChooserpdf.showOpenDialog(null);
				if (returnValue == JFileChooser.APPROVE_OPTION) {

					File selectedFile = fileChooserpdf.getSelectedFile();

					log.info("pdf folder path :" + selectedFile.getPath());

					if (!selectedFile.isDirectory()) {
						// 此处弹出对话框
						JOptionPane.showMessageDialog(Jpanel5.this,
								" 请选择存放  PDF 文件的文件夹");
						return;
					}

					try {

						// 不能计算加密的文件
						int pagenum = PDFUtils.getNumberOfPages(selectedFile);
						JOptionPane.showMessageDialog(Jpanel5.this, "\""
								+ selectedFile.getPath() + "\" 文件夹中 PDF 文件共有 "
								+ pagenum + " 页");

					} catch (MyMessageException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		
		add(buttonChoosePDFFolder, "2, 6");

		JLabel lblpdf = new JLabel(
				ResourceBundle
						.getBundle("swing.project.standard.messages").getString("Jpanel5.lblpdf.text")); //$NON-NLS-1$ //$NON-NLS-2$
		add(lblpdf, "5, 6, 24, 1");

		buttonChooseFile = new JButton(
				ResourceBundle
						.getBundle("swing.project.standard.messages").getString("Jpanel5.buttonChoseFile.text_1")); //$NON-NLS-1$ //$NON-NLS-2$
		buttonChooseFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				// 首先判断数据库是否可以访问，再进行下一步。只要有一个不可用，则弹出提示框
				// if (!SystemServiceMonitor.isNetServiceOpen("129.9.200.235",
				// 1521)
				// || !SystemServiceMonitor.isFtpInServiceOpen(
				// "129.9.200.236", 21, "read", "readpdf")) {
				//
				// // 此处弹出对话框
				// JOptionPane.showMessageDialog(Jpanel5.this,
				// " oracle 数据库或 ftp 不可用");
				// return;
				//
				// }

				Component parent = (Component) arg0.getSource();
				setEnabled(false);

				int returnValue = fileChooser.showOpenDialog(null);
				if (returnValue == JFileChooser.APPROVE_OPTION) {

					File selectedFile = fileChooser.getSelectedFile();
					log.info(selectedFile.getPath());

					add(infolabel, "2, 14, 27, 1");
					infolabel.setText("select : " + selectedFile.getPath());

					String downLoadDir = "d:\\ftpFiles\\"
							+ MyDateUtils.getToday(MyDateUtils
									.getToday("yyyy-MM-dd_HH-mm-ss")) + "\\";
					log.info("downLoadDir :" + downLoadDir);
					File downLoadDirf = new File(downLoadDir);

					try {
						MyFileUtils.forceMkdir(downLoadDirf);
						Runtime.getRuntime().exec(
								"rundll32 SHELL32.DLL,ShellExec_RunDLL "
										+ "Explorer.exe ," + downLoadDir);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					WorksUtils wu = new WorksUtils();

					wu.downLoadPDFFilesFromServer(
							new File(selectedFile.getPath()), downLoadDirf);

					// Jpanel1.this.button.setEnabled(false);
				}
				// button.setEnabled(true);
			}
		});

		add(buttonChooseFile, "2, 8, right, default");

		JLabel lbltxtpdf = new JLabel(
				ResourceBundle
						.getBundle("swing.project.standard.messages").getString("Jpanel5.lbltxtpdf.text")); //$NON-NLS-1$ //$NON-NLS-2$
		add(lbltxtpdf, "5, 8, 24, 1");

		JButton button = new JButton(
				ResourceBundle
						.getBundle("swing.project.standard.messages").getString("Jpanel5.btnPdf.text")); //$NON-NLS-1$ //$NON-NLS-2$
		add(button, "2, 10");

		JLabel lblpdf_1 = new JLabel(
				ResourceBundle
						.getBundle("swing.project.standard.messages").getString("Jpanel5.lblpdf_1.text")); //$NON-NLS-1$ //$NON-NLS-2$
		add(lblpdf_1, "5, 10, 24, 1");
	}
}